package hr.fer.ztel.rassus.brbljaonica.gui;

import hr.fer.ztel.rassus.brbljaonica.business.BusinessProvider;
import hr.fer.ztel.rassus.brbljaonica.business.HostedRoomPresentation;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.JTextPane;
import javax.swing.JButton;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.MultiUserChat;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Font;

/**
 * Klasa koja predstavlja prozor za grupne razgovore u sobi<br>
 * Stvara se odabirom sobe iz liste dostupnih soba ili<br>
 * kreiranjem nove sobe
 */
public class RoomChatWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextPane messagePane;
	private JTextArea textMessageArea;
	private MultiUserChat muc;
	private JList usersList;

	/**
	 * @wbp.parser.constructor
	 */
	public RoomChatWindow(MultiUserChat chat) {
		initGUI();
		muc = chat;
		muc.addMessageListener(new PacketListener() {

			@Override
			public void processPacket(Packet packet) {
				String message = ((Message) packet).getBody();
				messagePane.setText(messagePane.getText() + StringUtils.parseResource(packet.getFrom()) + ": " + System.getProperty("line.separator") + message + System.getProperty("line.separator"));
			}
		});
		muc.addParticipantListener(new PacketListener() {

			@Override
			public void processPacket(Packet packet) {
				Presence presence = (Presence) packet;
				DefaultListModel userListModel = (DefaultListModel) usersList.getModel();
				
				if (presence.isAvailable()) {
					userListModel.addElement(StringUtils.parseResource(presence.getFrom()));
				} else {
					userListModel.removeElement(StringUtils.parseResource(presence.getFrom()));
				}

				usersList.setModel(userListModel);
			}
		});
		setVisible(true);
	}
	
	public RoomChatWindow(HostedRoomPresentation room) {
		initGUI();
		muc = BusinessProvider.createMUC(room, this);
		setVisible(true);
	}

	private void initGUI() {
		setBounds(new Rectangle(0, 0, 600, 600));
		addWindowListener(new WindowListener() {
			@Override
			public void windowOpened(WindowEvent e) {
			}
			@Override
			public void windowIconified(WindowEvent e) {
			}
			@Override
			public void windowDeiconified(WindowEvent e) {
			}
			@Override
			public void windowDeactivated(WindowEvent e) {
			}
			@Override
			public void windowClosing(WindowEvent e) {
				muc.leave();
			}
			@Override
			public void windowClosed(WindowEvent e) {
			}
			@Override
			public void windowActivated(WindowEvent e) {
			}
		});
		getContentPane().setLayout(null);
		
		JScrollPane userListScrollPane = new JScrollPane();
		userListScrollPane.setBounds(10, 42, 110, 370);
		getContentPane().add(userListScrollPane);
		
		usersList = new JList();
		usersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		usersList.setModel( new DefaultListModel() );
		userListScrollPane.setViewportView(usersList);
		
		JScrollPane messageListScrollPane = new JScrollPane();
		messageListScrollPane.setBounds(131, 42, 394, 245);
		getContentPane().add(messageListScrollPane);
		
		messagePane = new JTextPane();
		messagePane.setEditable(false);
		messageListScrollPane.setViewportView(messagePane);
		
		JScrollPane textMessageScrollPane = new JScrollPane();
		textMessageScrollPane.setBounds(130, 331, 394, 80);
		getContentPane().add(textMessageScrollPane);
		
		textMessageArea = new JTextArea();
		textMessageArea.setFont(new Font("Tahoma", Font.PLAIN, 11));
		textMessageArea.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER && !e.isShiftDown()) {
					textMessageArea.setText("");
				}
			}
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					if (e.isShiftDown()) {
						textMessageArea.setText(textMessageArea.getText() + System.getProperty("line.separator"));
					}
					else {
					try {
						messagePane.setText(messagePane.getText() + "Me: " + System.getProperty("line.separator") + textMessageArea.getText() + System.getProperty("line.separator"));
						muc.sendMessage(textMessageArea.getText());
						textMessageArea.setText("");
					} catch (XMPPException e1) {
						e1.printStackTrace();
					}
					}
				}
			}
		});
		textMessageScrollPane.setViewportView(textMessageArea);
		
		JButton btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					muc.sendMessage(textMessageArea.getText());
					textMessageArea.setText("");
				} catch (XMPPException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnSend.setBounds(435, 422, 89, 23);
		getContentPane().add(btnSend);
		
		JLabel lblRoomParticipants = new JLabel("Room participants");
		lblRoomParticipants.setBounds(10, 17, 86, 14);
		getContentPane().add(lblRoomParticipants);
		
		JLabel lblMessages = new JLabel("Messages");
		lblMessages.setBounds(131, 17, 47, 14);
		getContentPane().add(lblMessages);
		
		JLabel lblMessageInput = new JLabel("Message input");
		lblMessageInput.setBounds(130, 306, 69, 14);
		getContentPane().add(lblMessageInput);
	}
	
	public JTextPane getTextMessagePane() {
		return messagePane;
	}
	
	public JList getUserList() {
		return usersList;
	}
}
